import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/ui/service_execute/service_execute_home_page.dart';
import 'package:ls_work/util/image_utils.dart';
import 'package:ls_work/viewmodel/service_execute/service_statistics_view_model.dart';

import 'new_service_execute_page.dart';

class ServiceStatisticsFragment extends BaseStatefulMvvmWidget {
  final String status;
  bool useNew;
  ServiceStatisticsFragment(this.status,{this.useNew = false});

  @override
  State<StatefulWidget> createState() => _ServiceStatisticsState();
}

class _ServiceStatisticsState extends BaseMvvmListState<
    ServiceStatisticsViewModel, ServiceStatisticsFragment> {
  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Column(
      children: [
        Container(
          height: 10,
        ),
        Row(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                CommonText(viewModel.selectedBuilding?.buildingNo ?? "全部楼栋"),
                Icon(Icons.arrow_drop_down_outlined)
              ],
            )
                .addToContainer(
                    padding: EdgeInsets.symmetric(vertical: 10),
                    decoration: BoxDecoration(
                        color: Colors.white, borderRadius: 8.borderRadius),
                    alignment: Alignment.center)
                .onTap(() {
              viewModel.selectBuilding();
            }).setWeight(1),
            Container(width: 10,),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                CommonText(viewModel.selectedFloor?.floorNo ?? "全部楼层"),
                Icon(Icons.arrow_drop_down_outlined)
              ],
            )
                .addToContainer(
                    padding: EdgeInsets.symmetric(vertical: 10),
                    decoration: BoxDecoration(
                        color: Colors.white, borderRadius: 8.borderRadius),
                    alignment: Alignment.center)
                .onTap(() {
              viewModel.selectFloor();
            }).setWeight(1),
          ],
        ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 16)),
        Row(
          children: [
            CommonInput(
              padding: EdgeInsets.zero,
              isDense: true,
              border: InputBorder.none,
              hintText: '输入房号或名字搜索',
              textAlign: TextAlign.center,
              textInputAction: TextInputAction.search,
              onSubmitted: (str) {
                viewModel.requestRefresh();
              },
              onChanged: (str) {
                viewModel.fuzzyQuery = str;
              },
            ).setWeight(1),
            Icon(Icons.search).onTap(() {
              viewModel.requestRefresh();
            })
          ],
        ).addToContainer(
            margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: 8.borderRadius)),
        loadingContentWidget.setWeight(1)
      ],
    );
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Row(
      children: [
        item.headimageId == null
            ? ImageLoad(
                ImageUtils.getImgPath(item.sex == 0 ? "nan" : "nv"),
                width: 60,
                height: 60,
                fit: BoxFit.cover,
                isAsset: true,
              ).toCircle()
            : ImageLoad(
                '${HttpApi.imageHead}${item.headimageId}',
                width: 60,
                height: 60,
                fit: BoxFit.cover,
              ).toCircle(),
        Container(
          width: 9,
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            CommonText(item.elderName,
                textSize: 17, textColor: Color(0xff313233)),
            Container(
              height: 15,
            ),
            CommonText("${item.bedNo}     ${item.nurseLevel}",
                textColor: Color(0xff626466), textSize: 15)
          ],
        ).setWeight(1),
        Stack(
          alignment: Alignment.center,
          children: [
            CircleProgressWidget(ProcessOptions(
                radius: 26,
                currentValue: (item.percentage??0) * 100,
                maxValue: 100,
                strokeColor:
                    item.percentage == 1 ? Colors.green : Colours.app_main,
                backgroundColor: Color(0xfff1f1f1))),
            item.percentage == 1
                ? Icon(
                    Icons.check,
                    color: Colors.green,
                  )
                : CommonText('${((item.percentage??0) * 100).toInt()}%',
                    textSize: 13)
          ],
        ).addToContainer(width: 56, height: 56)
      ],
    ).onTap(() {
      if(widget.useNew){
        Go().push(NewServiceExecutePage(
          customerElderId: item.customerElderId.toString(),
        ));
      }else{
        Go().push(ServiceExecuteHomePage(
          customerId: item.customerElderId.toString(),
        ));
      }
    }).addToContainer(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: 8.borderRadius,
        ),
        padding: EdgeInsets.symmetric(horizontal: 10, vertical: 15));
  }

  @override
  EdgeInsets get listPadding =>
      EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 80);

  @override
  bool get canPullUp => true;

  @override
  Widget get separatorDivider => Container(
        height: 10,
      );

  @override
  ServiceStatisticsViewModel createViewModel() {
    return ServiceStatisticsViewModel(
      context,
      widget.status,
    );
  }
}
